using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.Presenters.Views.Template;
using CMS.Presenters.Presenters.Template;
using CMS.Core.Domain;
using System.Collections.Generic;
using CMS.Data;
using ProjectBase.Utils;

namespace CMS.Web.Template
{
    public partial class BaiVietList : ViewUserControl, IBaiVietListView
    {
        IList<BaiViet> objIList;
        BaiVietListPresenter presenter;

        public BaiVietList()
        {
            this.presenter = new BaiVietListPresenter(this, NHibernateDaoFactory.Instance.GetBaiVietDao());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.presenter.GetAllBaiVietWithArticle(this.CatId);
            if (this.objIList.Count == 1)
            {
                Response.Redirect(this.BaseUrl + "?cid=" + this.CatId + "&vid=" + this.objIList[0].ID);
            }
            if (!this.IsPostBack)
            {
                this.grdArticles.DataSource = this.objIList;
                this.grdArticles.DataBind();
            }
            BaiVietTheLoai theLoai = new BaiVietTheLoaiDao().GetById(this.CatId);
            this.lblTenTheLoai.Text = theLoai.TheLoaiCha.TenTheLoai + " - " + theLoai.TenTheLoai;
        }

        protected void grdArticles_RowCreated(object sender, GridViewRowEventArgs e)
        {
            BaiViet baiviet = (BaiViet) e.Row.DataItem;
            if (baiviet != null)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    foreach (Control c in e.Row.Cells[0].Controls)
                    {
                        //the first article
                        if ((c.ClientID == "tblMainArticles"))
                        {
                            HtmlTable tbl = (HtmlTable)c;
                            c.Visible = true;

                            LinkButton lblTitle = (LinkButton)tbl.Rows[0].Cells[0].FindControl("lblTitle");
                            Label lblSummary = (Label)tbl.Rows[0].Cells[0].FindControl("lblSummary");
                            Image imgImage = (Image)tbl.Rows[0].Cells[0].FindControl("imgImage");
                            HtmlTable tblImg = (HtmlTable)tbl.Rows[0].Cells[0].FindControl("tblImg");

                            if (lblTitle != null)
                            {
                                lblTitle.Text = baiviet.TieuDe.ToString();
                                lblTitle.Attributes.Add("onclick", "return doView(\"" + this.BaseUrl + "?cid=" + this.CatId + "&vid=" + baiviet.ID.ToString() + "\");");
                            }
                           
                            if (lblSummary != null)
                            {
                                if ("".Equals(baiviet.TomTat.ToString()))
                                {
                                    lblSummary.Text = baiviet.TomTat;
                                }
                                else
                                {
                                    lblSummary.Text = baiviet.TomTat;
                                }
                            }

                            if ((baiviet.AnhDaiDien != "") && (CFile.Instance.ExistsFile("", baiviet.AnhDaiDien)))
                            {
                                tblImg.Visible = true;
                                imgImage.Visible = true;
                                imgImage.ImageUrl = this.FullBaseUrl +"sharedoc/" + baiviet.AnhDaiDien;
                            }
                            else
                            {
                                tblImg.Visible = false;
                                imgImage.Visible = false;
                            }

                        }
                    }
                }
            }


            if (e.Row.RowType == DataControlRowType.Pager)
            {
                foreach (Control c in e.Row.Cells[0].Controls)
                {
                    if (c.GetType().ToString().Equals("System.Web.UI.WebControls.PagerTable"))
                    {
                        Table tbl = (Table)c;
                        TableCell cell = new TableCell();
                        cell.Text = "Trang ";
                        cell.CssClass = "Gird_Paging";
                        cell.HorizontalAlign = HorizontalAlign.Right;
                        tbl.Rows[0].Cells.AddAt(0, cell);
                    }
                }
            }

        }       

        protected void grdArticles_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.grdArticles.DataSource = this.LstBaiViet;
            grdArticles.PageIndex = e.NewPageIndex;
            grdArticles.DataBind();
        }


        #region IBaiVietListView Members

        public IList<BaiViet> LstBaiViet
        {
            get { return this.objIList; }
            set { this.objIList = value; }
        }

        #endregion
    }
}